import 'package:flutter/material.dart';
import 'package:flutter_demo/utils/user_type.dart';

class AvatarRoleName extends StatelessWidget {
  final String coverPictureUrl;
  final String nickname;
  final String type;
  final bool showType;
  final double avatarSize;

  const AvatarRoleName({
    Key key,
    this.coverPictureUrl,
    this.nickname,
    this.type,
    this.showType = true,
    this.avatarSize = 25,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        _avatar(),
        Offstage(
          offstage: !showType,
          child: _role(),
        ),
        _nickname(),
      ],
    );
  }

  Widget _avatar() {
    return SizedBox(
      width: avatarSize,
      height: avatarSize,
      child: ClipOval(
        child: FadeInImage.assetNetwork(
            placeholder: 'assets/images/common/lazy-1.png',
            image: coverPictureUrl),
      ),
    );
  }

  Widget _role() {
    return Container(
      margin: EdgeInsets.only(left: 6),
      padding: EdgeInsets.symmetric(vertical: 2, horizontal: 4),
      decoration: BoxDecoration(
        color: UserType.fromColor(type),
        borderRadius: BorderRadius.circular(4),
      ),
      child: Text(
        UserType.fromCn(type) ?? '未知',
        style: TextStyle(fontSize: 10, color: Colors.white),
      ),
    );
  }

  Widget _nickname() {
    return Expanded(
        child: Container(
      margin: EdgeInsets.only(left: 6),
      child: Text(
        nickname,
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
      ),
    ));
  }
}
